package com.uphone.kingmall.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.activity.order.OrderDetailActivity;
import com.uphone.kingmall.bean.OrderBean;
import com.uphone.kingmall.utils.CommonUtil;

import java.util.List;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/25
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class RvOrderAdapter extends BaseQuickAdapter<OrderBean.DataBean, BaseViewHolder> {

    private final Context context;
    private final String type;
    private final OnShareClickListener onShareClickListener;

    /**
     * 订单适配器构造函数
     *
     * @param type
     * @param data
     */
    public RvOrderAdapter(Context context, String type, @Nullable List<OrderBean.DataBean> data, OnShareClickListener onShareClickListener) {
        super(R.layout.kingmall_item_order, data);
        this.context = context;
        this.type = type;
        this.onShareClickListener = onShareClickListener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final OrderBean.DataBean item) {
        RecyclerView recyclerView = helper.getView(R.id.rv_goods);
        TextView tvOrderState = helper.getView(R.id.tv_order_state);
        String time = item.getOrderTime();
        if (!TextUtils.isEmpty(time)) {
            String[] strs = time.split(" ");
            if (strs != null && strs.length > 0) {
                time = strs[0];
            }
        }
        StringBuffer sbTips = new StringBuffer();
        if (item.getPostType() == 1) {
            sbTips.append("含运费(￥" + item.getPostFee() + ")");
        }
        if (item.getInstallationFee() > 0) {
            sbTips.append("安装费(￥" + item.getInstallationFee() + ")");
        }
        helper.setText(R.id.tv_shop_name, item.getShopName())
                .setText(R.id.tv_date, time)
                .setText(R.id.tv_goods_number, "共" + item.getOrderGoodsNum() + "件商品")
                .setText(R.id.tv_price, item.getOrderFee() + "")
                .setText(R.id.tv_freight_price, sbTips.toString())
                .addOnClickListener(R.id.tv_qxdd)
                .addOnClickListener(R.id.tv_wyfk)
                .addOnClickListener(R.id.tv_wycd)
                .addOnClickListener(R.id.tv_lxsj)
//                .addOnClickListener(R.id.tv_qxdd2)
                .addOnClickListener(R.id.tv_sqtk)
                .addOnClickListener(R.id.tv_lxsj2)
                .addOnClickListener(R.id.tv_sqtk2)
                .addOnClickListener(R.id.tv_ckwl)
                .addOnClickListener(R.id.tv_qrsh)
                .addOnClickListener(R.id.tv_xfm)
                .addOnClickListener(R.id.tv_lxsj3)
                .addOnClickListener(R.id.tv_sqtk3)
                .addOnClickListener(R.id.tv_qpj);

        RelativeLayout rlBottom = helper.getView(R.id.rl_bottom);

        RelativeLayout rlDfk = helper.getView(R.id.rl_dfk);
        LinearLayout llDfh = helper.getView(R.id.rl_dfh);
        LinearLayout llDshOnline = helper.getView(R.id.rl_dsh_online);
        RelativeLayout rlDsh = helper.getView(R.id.rl_dsh);
        RelativeLayout rlDpj = helper.getView(R.id.rl_dpj);
        rlDfk.setVisibility(View.GONE);
        llDfh.setVisibility(View.GONE);
        llDshOnline.setVisibility(View.GONE);
        rlDsh.setVisibility(View.GONE);
        rlDpj.setVisibility(View.GONE);
        rlBottom.setVisibility(View.VISIBLE);
        switch (type) {
            case OrderBean.DFX://待分享
                tvOrderState.setText("待分享");
                rlBottom.setVisibility(View.GONE);
                break;
            case OrderBean.DFK://待付款
                tvOrderState.setText("待付款");
                rlDfk.setVisibility(View.VISIBLE);
                break;
            case OrderBean.DFH://待发货
                tvOrderState.setText("待发货");
                llDfh.setVisibility(View.VISIBLE);
                break;
            case OrderBean.DSH://待收货
                tvOrderState.setText("待收货");
                if (item.getPostType() == 1) {
                    llDshOnline.setVisibility(View.VISIBLE);
                } else {
                    rlDsh.setVisibility(View.VISIBLE);
                }
                break;
            case OrderBean.DPJ://待评价
                tvOrderState.setText("待评价");
                rlDpj.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

        RvOrderGoodsAdapter goodsAdapter = new RvOrderGoodsAdapter(context, type == OrderBean.DFX ? true : false, item.getGoodslist());
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(goodsAdapter);
        goodsAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_order_share) {
                    onShareClickListener.onShareClickListener(helper.getAdapterPosition(), position);
                }
            }
        });
        goodsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CommonUtil.startIntent(mContext, OrderDetailActivity.class, item.getOrderId());
            }
        });
    }

    public interface OnShareClickListener {
        void onShareClickListener(int shopPosition, int goodsPosition);
    }
}
